$(function(){
	$( "#birthday").datepicker({
			showOn: "button",
			buttonImage: "../images/calendar.gif",
			buttonImageOnly: true
	});
	$( "#birthday").datepicker( "option", "dateFormat", "yy-mm-dd" );
	// 手机号码验证       
	jQuery.validator.addMethod("isMobile", function(value, element) {       
		var length = value.length;   
		var mobile = /^(13[0-9]|15[0-9]|18[0-9]|14[0-9])\d{8}$/;   
		return this.optional(element) || (length == 11 && mobile.test(value)); 
	}, "请正确填写您的手机号码");
	jQuery.validator.addMethod("isChineseName",function(value,element){
		var nameReg = new RegExp("^[\u4e00-\u9fa5]+$");
		return this.optional(element) || nameReg.test(value);
	},"请输入中文名字");
	jQuery.validator.addMethod("isIDCard",function(value,element){
		return new RegExp("^([0-9]|[a-z]|[A-Z]){18}$").test(value);
	},"请输入合法身份证号码");
	$("#form1").validate({
		rules: {
			truename:{
				required:true,
				isChineseName:true
			},
			idcard: {
				isIDCard:true
			},
			telephone: {
				required:true,
				isMobile:true
			},
			email: {
				required: true,
				email: true
			},
			birthday:{
				required:true
			}
		},
		messages: {
			truename:{
				required:"请输入您的真实姓名",
			},
			telephone:{
				required:"请输入正确的手机号码"
			},
			email:{
				required:"请输入正确的邮箱地址"
			},
			birthday:{
				required:"请选择出生日期"
			}
		},
		highlight: function(element) {
			$(element).removeClass('success').addClass('error');
			//当某个input验证成功后再失败的话，添加的ok类依然存在
			$(element).parent().next().children('label.ok').removeClass('ok');
		},
		// the errorPlacement has to take the layout into account
		errorPlacement: function(error, element) {
			error.appendTo(element.parent().next());
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function(form) {
			alert("Data submitted!");
			form.submit();
		},
		// set new class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("ok");
		}
	});
});
